<?php

App::uses('AppController', 'Controller');

class UsersController extends AppController {

    public $uses = array('User');
    
    public $title = 'Korisnici';
    
    public function index() {
        $this->set('users', $this->User->find('all'));
    }
    
    protected function setCommonData() {
        $this->set('roles', array('admin'=>'Administrator', 'user'=>'Korisnik'));
    }
    
    public function create() {
        $this->set('data', $this->User->create());
        $this->setCommonData();
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('data', $this->User->findById($id));
        $this->setCommonData();
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            // check password change
            $pass1 = $data['User']['password'];
            $pass2 = $data['User']['password2'];
            if (!empty($pass1) && !empty($pass2)) {
                if ($pass1 != $pass2) {
                    $message = 'Lozinke se ne podudaraju.';
                    $this->flashError($message);
                    $this->User->validationErrors['password'] = $message;
                    $this->User->validationErrors['password2'] = $message;
                    $this->setCommonData();
                    $this->set('data', $data);
                    return $this->render('form');
                } else {
                    $data['User']['password'] = $this->Auth->password($pass1);
                }
            } else {
                unset($data['User']['password']);
            }
            
            $this->User->create();
            $this->User->set($data);
            
            if ($this->User->validates()) {
                if ($this->User->save($data)) {
                    $this->flashOk('Podaci korisnika su sačuvani.');
                    $this->redirect('/users/edit/' . $this->User->id);
                } else {
                    $this->flashError('Čuvanje neuspelo.');
                    $this->setCommonData();
                    $this->set('data', $data);
                    return $this->render('form');
                }
            } else {
                $this->flashError('Uneti podaci nisu validni.');
                $this->setCommonData();
                $this->set('data', $data);
                return $this->render('form');
            }
        }
        
        $this->redirect('index');
    }
    
    public function delete($id) {
        if ($id>0) {
            if ($this->User->delete($id)) {
                $this->flashOk('Korisnik je obrisan.');
            } else {
                $this->flashError('Korisnik nije obrisan.');
            }
        }
        $this->redirect('index');
    }
    
    public function login() {
        $this->layout = 'login';
        $data = $this->data;
        if (! empty($data)) {
            $login = $this->Auth->login();
            if (! $login) {
                $this->flashError('Prijava nije uspela.');
            } else {
                $userId = $this->Auth->user('id');
                $user   = $this->User->findById($userId);
                $this->User->save(array(
                    'lastlogin' => date('Y-m-d H:i:s'),
                    'id'        => $userId
                ));
                $this->Session->write("CurrentUser", $user);
                $this->setStore($user['Store'][0]['id'], false);
            }
            return $this->redirect("/");
        }
    }
    
    public function logout() {
        $this->redirect($this->Auth->logout());
    }

}